<template>
  <view class="print-demo-container">
    <view class="page-header">
      <text class="page-title">PDA打印测试</text>
    </view>
    
    <view class="demo-section">
      <view class="form-item">
        <text class="form-label">编码</text>
        <input 
          class="form-input" 
          v-model="testData.code" 
          placeholder="请输入编码（如批号）"
        />
      </view>
      
      <view class="form-item">
        <text class="form-label">名称</text>
        <input 
          class="form-input" 
          v-model="testData.name" 
          placeholder="请输入名称（如钢种）"
        />
      </view>
      
      <view class="form-item">
        <text class="form-label">备注</text>
        <input 
          class="form-input" 
          v-model="testData.remark" 
          placeholder="请输入备注（如规格）"
        />
      </view>
      
      <view class="button-group">
        <button class="primary-btn" @click="testPrint">测试打印</button>
        <button class="secondary-btn" @click="testPreview">预览图片</button>
      </view>
    </view>
    
    <view class="info-section">
      <text class="info-title">使用说明</text>
      <text class="info-text">1. 填写编码、名称和备注信息</text>
      <text class="info-text">2. 点击"测试打印"生成二维码并执行PDA打印</text>
      <text class="info-text">3. 点击"预览图片"仅查看生成的图片</text>
      <text class="info-text">4. 打印规格：58mm热敏纸，384x200像素</text>
    </view>
    
    <!-- 隐藏的Canvas元素 -->
    <canvas 
      canvas-id="printQRCanvas" 
      style="width: 384px; height: 200px; position: fixed; left: -9999px; top: -9999px; visibility: hidden;"
      :style="{ width: '384px', height: '200px' }"
    ></canvas>
  </view>
</template>

<script>
export default {
  data() {
    return {
      testData: {
        code: 'P123456789',
        name: '六角螺栓 M8×30',
        remark: '批次 20250625'
      }
    }
  },
  
  methods: {
    async testPrint() {
      try {
        const { code, name, remark } = this.testData
        
        if (!code || !name) {
          uni.showToast({
            title: '编码和名称不能为空',
            icon: 'none'
          })
          return
        }
        
        uni.showLoading({
          title: '生成打印图片...',
          mask: true
        })
        
        // 动态导入打印模块
        const { simplePrintQRcode } = await import('@/pages/smart/printPage/printQRcode.js')
        
        // 生成打印图片
        const tempImagePath = await simplePrintQRcode({
          code,
          name,
          remark
        })
        
        uni.hideLoading()
        
        // 执行PDA打印
        await this.printWithPDA(tempImagePath)
        
        uni.showToast({
          title: '打印指令已发送',
          icon: 'success'
        })
        
      } catch (error) {
        console.error('打印失败:', error)
        uni.hideLoading()
        uni.showToast({
          title: error.message || '打印失败',
          icon: 'error'
        })
      }
    },
    
    async testPreview() {
      try {
        const { code, name, remark } = this.testData
        
        if (!code || !name) {
          uni.showToast({
            title: '编码和名称不能为空',
            icon: 'none'
          })
          return
        }
        
        uni.showLoading({
          title: '生成预览图片...',
          mask: true
        })
        
        // 动态导入打印模块
        const { simplePrintQRcode } = await import('@/pages/smart/printPage/printQRcode.js')
        
        // 生成打印图片
        const tempImagePath = await simplePrintQRcode({
          code,
          name,
          remark
        })
        
        uni.hideLoading()
        
        // 预览图片
        uni.previewImage({
          urls: [tempImagePath],
          current: tempImagePath
        })
        
      } catch (error) {
        console.error('预览失败:', error)
        uni.hideLoading()
        uni.showToast({
          title: error.message || '预览失败',
          icon: 'error'
        })
      }
    },
    
    // PDA打印功能
    async printWithPDA(imagePath) {
      try {
        console.log('开始PDA热敏打印...')
        
        if (typeof plus !== 'undefined' && plus.android) {
          // 方案1: 使用PDA原生printImage接口
          if (plus.device && plus.device.printImage) {
            plus.device.printImage(imagePath, {
              width: 384,
              align: 'center',
              autoCut: true,
              copies: 1
            })
            console.log('PDA原生打印接口调用成功')
            return
          }
          
          // 方案2: 使用Android Intent调用系统打印
          const Intent = plus.android.importClass('android.content.Intent')
          const Uri = plus.android.importClass('android.net.Uri')
          const File = plus.android.importClass('java.io.File')
          
          try {
            const imageFile = new File(imagePath)
            const imageUri = Uri.fromFile(imageFile)
            const printIntent = new Intent('android.intent.action.PRINT')
            printIntent.setDataAndType(imageUri, 'image/*')
            printIntent.putExtra('scaleType', 'fit')
            
            plus.android.runtimeMainActivity().startActivity(printIntent)
            console.log('PDA Intent打印调用成功')
            
          } finally {
            plus.android.deleteObject(Intent)
            plus.android.deleteObject(Uri)
            plus.android.deleteObject(File)
          }
        } else {
          console.log('非PDA环境，跳过打印')
          uni.showToast({
            title: '当前环境不支持PDA打印',
            icon: 'none'
          })
        }
        
      } catch (error) {
        console.error('PDA打印失败:', error)
        throw error
      }
    }
  }
}
</script>

<style scoped lang="scss">
.print-demo-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  text-align: center;
  margin-bottom: 40rpx;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.demo-section {
  background: white;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
}

.form-item {
  margin-bottom: 30rpx;
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.form-input {
  width: 100%;
  height: 80rpx;
  border: 2rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 30rpx;
  background: #fafafa;
}

.button-group {
  display: flex;
  gap: 20rpx;
  margin-top: 40rpx;
}

.primary-btn, .secondary-btn {
  flex: 1;
  height: 80rpx;
  border: none;
  border-radius: 8rpx;
  font-size: 30rpx;
  font-weight: 500;
}

.primary-btn {
  background: #007aff;
  color: white;
}

.secondary-btn {
  background: #f0f0f0;
  color: #333;
}

.info-section {
  background: white;
  border-radius: 12rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
}

.info-title {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.info-text {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin-bottom: 10rpx;
  line-height: 1.5;
}
</style>